﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Other_Other : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadOtherAssetDetails();
            setEditableFalse();
        }

    }

    private void loadOtherAssetDetails()
    {
        int otherAsset_ID = Int32.Parse((String)Session["Other_ID"]);

        Other other = new Other();
        OtherDBConnection otherConn = new OtherDBConnection();
        other = otherConn.getOtherAssetByID(otherAsset_ID);

        HardwareDBConnection hardwareConn = new HardwareDBConnection();
        Hardware hardware = hardwareConn.getHardwareByID(other.hardware_ID);

        RoomDBConnection roomConn = new RoomDBConnection();
        Room classroom = roomConn.getRoomByNo(hardware.room_no);

        txtAssetID.Text = other.other_asset_ID.ToString();
        txtAssetName.Text = other.asset_Name;
        txtBrandNamel.Text = other.brand_Name;
        txtDescription.Text = other.description;
        txtLocation.Text = classroom.room_Name;
        //txtPerson.Text = "";
        txtPurchaseDate.Text = hardware.purchase_date.ToString();
        txtSerial.Text = hardware.serial;
        txtType.Text = other.type;

        WarrantyDBConnection warrantyConn = new WarrantyDBConnection();
        Warranty warranty = warrantyConn.getWarrantyById(hardware.warranty_ID);
        linkWarrenty.Text = warranty.warranty_Status;



    }
    
    protected void WarrantyLinkButton_Click(object sender, EventArgs e)
    {
        OtherDBConnection otherConn = new OtherDBConnection();
        Other other = otherConn.getOtherAssetByID(Int32.Parse(txtAssetID.Text));

        HardwareDBConnection hardwareConn = new HardwareDBConnection();
        Hardware hardware = hardwareConn.getHardwareByID(other.hardware_ID);

        Session["Warranty_ID"] = hardware.warranty_ID;
        Response.Redirect("../Warranty/Warranty.aspx", true);
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        OtherDBConnection otherConn = new OtherDBConnection();
        Other otherAsset = otherConn.getOtherAssetByID(Int32.Parse(txtAssetID.Text));

        RoomDBConnection roomConn = new RoomDBConnection();
        Room room = roomConn.getRoomByName(ddlLocation.SelectedValue);

        HardwareDBConnection hardwareConn = new HardwareDBConnection();
        Hardware hardware = hardwareConn.getHardwareByID(otherAsset.hardware_ID);
        hardware.room_no = room.room_No;
        hardwareConn.update(hardware);
        txtLocation.Text = room.room_Name;




        //dbConnection.update(customer);

        setEditableFalse();
        btnUpdate.Visible = false;
        btnCancle.Visible = false;
        btnEdit.Visible = true;
        btnDelete.Visible = true;


        txtLocation.Visible = true;
        ddlLocation.Visible = false;
        lbtnLocation.Visible = false;
    }
    protected void btnCancle_Click(object sender, EventArgs e)
    {
        setEditableFalse();
        btnUpdate.Visible = false;
        btnCancle.Visible = false;
        btnEdit.Visible = true;
        btnDelete.Visible = true;


        //txtLocation.Visible = true;
        //ddlLocation.Visible = false;
        //lbtnLocation.Visible = false;
        //lbtnPerson.Visible = false;


        txtLocation.Visible = true;
        ddlLocation.Visible = false;
        lbtnLocation.Visible = false;
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        setEditableTrue();
        btnUpdate.Visible = true;
        btnCancle.Visible = true;
        btnEdit.Visible = false;
        btnDelete.Visible = false;

        txtLocation.Visible = false;
        ddlLocation.Visible = true;
        lbtnLocation.Visible = true;
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        OtherDBConnection otherConn = new OtherDBConnection();
        Other other = otherConn.getOtherAssetByID(Int32.Parse(txtAssetID.Text));

        HardwareDBConnection hardwareConn = new HardwareDBConnection();
        Hardware hardware = hardwareConn.getHardwareByID(other.hardware_ID);
        hardwareConn.delete(hardware);

        FeedBack feedback = new FeedBack();
        feedback.success = 1;
        feedback.lastPageURL = "Assets.aspx";
        feedback.message = "You have successfully deleted the asset: '" +
                            other.asset_Name + " (" + other.other_asset_ID + ")' of type other asset.";
        Session["Feedback"] = feedback;
        Server.Transfer("../Feedback.aspx");

    }



    private void setEditableFalse()
    {
        txtAssetID.ReadOnly = true;
        txtAssetName.ReadOnly = true;
        txtBrandNamel.ReadOnly = true;
        txtDescription.ReadOnly = true;
        txtLocation.ReadOnly = true;
        //txtPerson.ReadOnly = true;
        txtPurchaseDate.ReadOnly = true;
        txtSerial.ReadOnly = true;
        //txtWarrenty.ReadOnly = true;
        txtType.ReadOnly = true;
        txtBrandNamel.ReadOnly = true;

        txtAssetID.BorderStyle = BorderStyle.None;
        txtAssetName.BorderStyle = BorderStyle.None;
        txtDescription.BorderStyle = BorderStyle.None;
        txtLocation.BorderStyle = BorderStyle.None;
        //txtPerson.BorderStyle = BorderStyle.None;
        txtPurchaseDate.BorderStyle = BorderStyle.None;
        txtSerial.BorderStyle = BorderStyle.None;
        //txtWarrenty.BorderStyle = BorderStyle.None;
        txtType.BorderStyle = BorderStyle.None;
        txtBrandNamel.BorderStyle = BorderStyle.None;
    }
    private void setEditableTrue()
    {
        //txtComputerID.ReadOnly = false;
        //txtComputerName.ReadOnly = false;
        //txtLicense.ReadOnly = false;
        txtLocation.ReadOnly = false;
        //txtPerson.ReadOnly = false;
        //txtPurchaseDate.ReadOnly = false;
        //txtSerial.ReadOnly = false;
        //txtWarrenty.ReadOnly = false;

        //txtComputerID.BorderStyle = BorderStyle.NotSet;
        //txtComputerName.BorderStyle = BorderStyle.NotSet;
        //txtLicense.BorderStyle = BorderStyle.NotSet;
        txtLocation.BorderStyle = BorderStyle.NotSet;
        //txtPerson.BorderStyle = BorderStyle.NotSet;
        //txtPurchaseDate.BorderStyle = BorderStyle.NotSet;
        //txtSerial.BorderStyle = BorderStyle.NotSet;
        //txtWarrenty.BorderStyle = BorderStyle.NotSet;
    }





}